package com.example.administrator.zhongyichou.activity;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.zhongyichou.Bean.BaseEventBean;
import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.base.MyApplication;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.net.NetConfig;
import com.example.administrator.zhongyichou.net.core.RequestHttpLoader;
import com.example.administrator.zhongyichou.utils.ToastUtil;
import com.example.administrator.zhongyichou.net.core.HttpLoader;
import com.example.administrator.zhongyichou.net.listener.ResultListener;
import com.example.administrator.zhongyichou.net.protocol.GsonBaseProtocol;
import com.example.administrator.zhongyichou.net.protocol.LoginCodeProtocol;
import com.example.administrator.zhongyichou.view.HeaderView;
import com.example.administrator.zhongyichou.R;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lichenyu on 2017/12/1.
 * 描述：验证手机号
 */

public class VerificationPhoneActivity extends BaseActivity<LoginCodeProtocol> {
    @BindView(R.id.headerview)
    HeaderView headerview;
    @BindView(R.id.txt_phone)
    TextView txtPhone;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.txt_getcode)
    TextView txtGetcode;
    private CountDownTimer countDownTimer;
    private String phone;
    private String id;
    private String code;

    @Override
    protected void init() {
        ButterKnife.bind(this);
        initHead();
        initTime();
    }

    @Override
    protected void responseData(LoginCodeProtocol mGsonBaseProtocal) {
        ToastUtil.showToast("验证码" + mGsonBaseProtocal.getData().getMsg_code());
        txtGetcode.setEnabled(false);
        countDownTimer.start();
        txtPhone.setText(phone);
    }

    @Override
    protected HttpLoader initHttpLoader() {
        phone = getIntent().getStringExtra("phone");
        id = getIntent().getStringExtra("id");
        Map<String, String> map = new HashMap<>();
        map.put("user_tel", phone);
        map.put("msg_type", "1");
        RequestHttpLoader httpLoader = new RequestHttpLoader(LoginCodeProtocol.class);
        httpLoader.setParams(NetConfig.GET_CODE, map);
        return httpLoader;
    }

    @Override
    protected boolean isRequestData() {
        return true;
    }

    @Override
    protected int initContentView() {
        return R.layout.verification_phone_activity;
    }

    @Override
    public boolean onEventBus(BaseEventBean baseEventBean) {
        return false;
    }

    @OnClick({R.id.txt_getcode, R.id.btn_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_getcode:
                getCode();
                break;
            case R.id.btn_finish:
                code = editCode.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showToast("请填写验证码");
                    return;
                }
                activeCard();
                break;
        }
    }

    private void initHead() {
        headerview.setTitle("验证手机号");
        headerview.bindOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case HeaderView.LEFT:
                        finish();
                        break;
                }
            }
        });
    }

    private void initTime() {
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                millisUntilFinished = millisUntilFinished / 1000;
                txtGetcode.setText(Math.round(millisUntilFinished) + "秒");
            }

            @Override
            public void onFinish() {
                txtGetcode.setText("获取验证码");
                txtGetcode.setEnabled(true);
            }
        };
    }

    private void getCode() {
        Map<String, String> map = new HashMap<>();
        map.put("user_tel", phone);
        map.put("msg_type", "1");
        RequestHttpLoader httpLoader = new RequestHttpLoader(LoginCodeProtocol.class);
        httpLoader.setParams(NetConfig.GET_CODE, map);
        httpLoader.executor(new ResultListener<LoginCodeProtocol>() {
            @Override
            public void success(LoginCodeProtocol mGsonBaseProtocol) {
                ToastUtil.showToast("验证码" + mGsonBaseProtocol.getData().getMsg_code());
                txtGetcode.setEnabled(false);
                countDownTimer.start();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }

    /**
     * 激活银行卡
     */
    private void activeCard() {
        RequestHttpLoader requestHttpLoader = new RequestHttpLoader(GsonBaseProtocol.class);
        Map<String, String> map = new HashMap<>();
        map.put("user_id", MyApplication.getUserID());
        map.put("card_id", id);
        map.put("msg_code", code);
        requestHttpLoader.setParams(NetConfig.ACTIVE_CARD, map);
        requestHttpLoader.showDialog(this, true, "正在提交");
        requestHttpLoader.executor(new ResultListener() {
            @Override
            public void success(GsonBaseProtocol mGsonBaseProtocol) {
                EventBus.getDefault().post(new BaseEventBean<>(Constants.TYPE_3));
                ToastUtil.showToast("验证成功");
                finish();
            }

            @Override
            public void error(String mErrorProtocal) {

            }

            @Override
            public void notNet() {

            }

            @Override
            public void requestError() {

            }
        });
    }
}
